﻿Ext.define('app.prm.project.ProjectWeekly', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    filter: { WeekTime: new Date(), WeekType: 'Now' },
    rdCenterId: 0,
    initComponent: function () {
        var me = this;
        me.items = [me.getWest(), me.getWeekPanel()];
        me.callParent();
    },
    getWest: function () {
        var me = this;
        if (!this.west) {
            var store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/rdCenter/GetAll' });
            store.on("load", function (store, records, successful, eOpts) { me.west.getSelectionModel().select(0); });
            this.west = new Ext.grid.Panel({
                border: true,
                region: 'west',
                width: 200,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                multiColumnSort: true,
                forceFit: true,
                store: store,
                columns: [
                    { header: '研发中心', dataIndex: 'name' }
                ],
                listeners: {
                    select: function (grid, record, index, eOpts) {
                        me.rdCenterId = record.data.id;
                        me.updateWeek('Now');
                    }
                }
            });
        }
        return this.west;
    },
    getWeekPanel: function () {
        var me = this;
        if (!this.weeklyPanel) {
            this.weeklyPanel = new Ext.panel.Panel({
                border: false,
                title: '',
                tbar: [
                    { text: '上一周', glyph: 0xf100, handler: function () { me.updateWeek('上一周'); } },
                    { text: '下一周', glyph: 0xf101, handler: function () { me.updateWeek('下一周'); } }
                ],
                region: 'center',
                autoScroll: true,
                items: []
            });
        }
        return this.weeklyPanel;
    },
    updateWeek: function (weekType) {
        var me = this;
        me.filter.WeekType = weekType;
        App.AjaxPost('/api/services/app/summary/GetYearAndWeek', me.filter, function (data) {
            var record = data.result;
            me.filter.WeekTime = record.startTime;
            me.getWeekPanel().setTitle(record.year + "年" + record.week + "周" + "&nbsp;(" + record.startTime + "&nbsp;~&nbsp;" + record.endTime + ")");

            me.getWeekPanel().removeAll(true);

            App.AjaxGet('/api/services/app/laboratory/GetByCenterId?rdCenterId=' + me.rdCenterId, function (data) {
                console.log(data);
                for (var i = 0; i < data.result.length; i++) {
                    var rec = data.result[i];
                    var panel = Ext.create('app.prm.project.WeeklyView', { laboratoryId: rec.id, startTime: record.startTime });
                    me.getWeekPanel().add(panel);
                }
                //me.doLayout();
            });
        });
    }
});